$(document).ready(function () {
    var main = {
        sharingPhotos : [] ,
        canceledPhotos : [] ,
        init : function () {
            this.getSharingPhotos();
            this.fileUploadInit();
            this.events();
            $('.all-photos-operation').css("left" , (window.screen.width - 1205) / 2 + 114);
        } ,
        fileUploadInit : function () {
            var uploaderedit = new plupload.Uploader({
                runtimes : 'html5,flash,silverlight,html4' ,
                browse_button : 'uploadEditPhoto' ,
                url : "/snapshot/shop/uploadImage.do" ,
                multipart : true ,
                filters : {
                    max_file_size : '5mb' ,
                    mime_types : [
                        {title : "Image files" , extensions : "jpg,gif,png"} ,
                        {title : "Zip files" , extensions : "zip"}
                    ] ,
                    prevent_duplicates : true
                } ,
                multi_selection : true ,
                preinit : function (uploader) {
                    uploader.bind('FilesAdded' , function (up , file , response) {
                        alert("图片上传成功，刷新可见!");
                    });
                } ,
                init : {
                    FilesAdded : function (up , files) {
                        uploaderedit.start();
                    }
                }
            });
            uploaderedit.init();
        } ,
        getSharingPhotos : function () {
            var self = this;
            if (self.getSharingPhotosDone) {
                $('.all-photos-page').html(template.render("photo-template" , {photos : self.sharingPhotos}));
            } else {
                $.ajax({
                    url : '/snapshot/image/selectShopPublicImages.do' ,
                    data : {
                        shopOpenId : 100000 ,
                        size : 10000 ,
                        no : 1 ,
                        state : 3
                    } ,
                    dataType : "json" ,
                    success : function (resp) {
                        if (resp.success && resp.value && resp.value.shopPublicImages.length) {
                            self.sharingPhotos = resp.value.shopPublicImages;
                            $('.all-photos-page').html(template.render("photo-template" , {photos : self.sharingPhotos}));
                            self.getSharingPhotosDone = true;
                        } else {
                            $('.all-photos-page').html("<p class = 'tc f16' style = 'color:#89f'>暂无相关数据</p>");
                        }
                    }
                });
            }
        } ,
        getCanceledPhotos : function () {
            var self = this;
            if (self.getCanceledPhotosDone) {
                $('.all-photos-page').html(template.render("photo-template" , {photos : self.canceledPhotos}));
            } else {
                $.ajax({
                    url : '/snapshot/image/selectShopPublicImages.do' ,
                    data : {
                        shopOpenId : 100000 ,
                        size : 10000 ,
                        no : 1 ,
                        state : 2
                    } ,
                    dataType : "json" ,
                    success : function (resp) {
                        if (resp.success && resp.value && resp.value.shopPublicImages.length) {
                            self.canceledPhotos = resp.value.shopPublicImages;
                            $('.all-photos-page').html(template.render("photo-template" , {photos : self.canceledPhotos}));
                            self.getCanceledPhotosDone = true;
                        } else {
                            $('.all-photos-page').html("<p class = 'tc f16' style = 'color:#89f'>暂无相关数据</p>");
                        }
                    }
                });
            }
        } ,
        changePhotoState : function (from , to) {
            var self = this;
            var imageIds = [];
            $('.all-photos-page .photo').each(function () {
                var $photo = $(this);
                if ($photo.find('input').is(':checked')) {
                    var photo = self[from].splice($photo.index() , 1);
                    self[to].push(photo[0]);
                    imageIds.push($photo.attr('pid'));
                    $photo.remove();
                }
            });
            $.ajax({
                url : '/snapshot/shop/updateShopImage.do' ,
                data : {
                    state : from == 'sharingPhotos' && to == 'canceledPhotos' ? 2 : 1 ,
                    imageIds : imageIds.join(';')
                } ,
                dataType : 'json' ,
                success : function (resp) {
                    if (resp.success) {
                        if (from == 'sharingPhotos' && to == 'canceledPhotos') {
                            main.getSharingPhotos();
                        } else {
                            main.getCanceledPhotos();
                        }
                    }
                }
            });
        } ,
        events : function () {
            $('.nav-buttons li').click(function () {
                $(this).addClass('active').siblings().removeClass('active');
            });
            $('#sharingPhotos').click(function () {
                $('.cancel-share').show().siblings('.recover-share').hide();
                main.getSharingPhotos();
                $('.change-photo-state').text("取消分享");
            });
            $('#canceledPhotos').click(function () {
                $('.cancel-share').hide().siblings('.recover-share').show();
                main.getCanceledPhotos();
                $('.change-photo-state').text("恢复分享");
            });
            $('.all-photos-page').delegate('.photo' , 'click' , function (e) {
                var $photo = $(e.target).closest('.photo');
                if ($photo.hasClass('selected')) {
                    $photo.removeClass('selected');
                } else {
                    $photo.addClass('selected');
                }
                $photo.find('input').click();
            });
            $('.all-photos-page').delegate('input' , 'click' , function (e) {
                e.stopPropagation();
            });
            $('.select-all').click(function () {
                $('.all-photos-page .photo').each(function () {
                    var $photo = $(this);
                    if (!$photo.hasClass('selected')) {
                        $photo.addClass('selected');
                        $photo.find('input').click();
                    }
                });
            });
            $('.anti-select').click(function () {
                $('.all-photos-page .photo').each(function () {
                    var $photo = $(this);
                    if ($photo.hasClass('selected')) {
                        $photo.removeClass('selected');
                    } else {
                        $photo.addClass('selected');
                    }
                    $photo.find('input').click();
                });
            });
            $('.change-photo-state').click(function () {
                if ($(this).text() == '取消分享') {
                    main.changePhotoState('sharingPhotos' , 'canceledPhotos');
                } else {
                    main.changePhotoState("canceledPhotos" , 'sharingPhotos');
                }
            });
        } ,
        login : function () {
            $.ajax({
                url : '/snapshot/home/loginShop.do' ,
                data : {
                    openId : 100000
                } ,
                dataType : 'json' ,
                success : function (resp) {
                    if (resp.success) {
                        main.init();
                    } else {
                        alert(resp.message);
                    }
                }
            });
        }
    }
    main.login();
});